package cn.ac.iscas.service.transformer;

import cn.ac.iscas.base.utils.ParameterUtils;
import cn.ac.iscas.base.utils.SpringUtil;
import cn.ac.iscas.domain.source.FlinkSourceColumn;
import cn.ac.iscas.service.queue.flink.FlinkTableQueueService;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * @author LJian
 * @version 1.0
 * @description: TODO
 * @date 2022/10/8 10:10
 */
public class DeduplicationTransformer {
    private final static String ID = "deduplication";
    private final static String COLUMN_KEY = "column";
    private String jobId;
    private String componentId;
    private String frontTableName;
    private List<FlinkSourceColumn> frontColumns;

    private String currentTableName;
    private List<FlinkSourceColumn> currentColumns;

    public DeduplicationTransformer(String jobId, String taskId) {
        this.jobId = jobId;
        this.componentId = taskId;
    }

    public DeduplicationTransformer(String jobId, String taskId, String frontTableName, List<FlinkSourceColumn> frontColumns) {
        this.jobId = jobId;
        this.componentId = taskId;
        this.frontTableName = frontTableName;
        this.frontColumns = frontColumns;
        this.currentColumns = frontColumns;
        this.currentTableName = String.format("%s.%s.%s_%s", jobId, jobId, ID, UUID.randomUUID().toString().substring(0,6));
    }


    public DeduplicationTransformer() {

    }

    //SELECT * FROM ( SELECT *, ROW_NUMBER() OVER (PARTITION BY name ORDER BY name ASC) AS row_num FROM c81a9eee.c81a9eee.ods_classtable) WHERE row_num = 1;
    public List<String> getSql(Map<String, Object> parameters) {
        List<String> sqls = new ArrayList<>();
        String tmpTableName = this.currentTableName + "tmp";
        String createViewSqlTmp = String.format("CREATE VIEW %s AS ", tmpTableName);
        String column = ParameterUtils.getRequiredString(parameters, COLUMN_KEY);
        String selectSqlTmp = String.format(" ( SELECT * FROM ( SELECT *, ROW_NUMBER() OVER (PARTITION BY %s ORDER BY %s ASC) AS row_num FROM %s) WHERE row_num = 1 ) ", column, column, frontTableName);
        createViewSqlTmp += selectSqlTmp;

        String createViewSql = String.format("CREATE VIEW %s AS ", this.currentTableName);
        String columnsSql = (this.frontColumns.stream().map(FlinkSourceColumn::getName).collect(Collectors.joining(",")));
        String selectSql = String.format("( SELECT %s FROM %s )", columnsSql, tmpTableName);
        createViewSql += selectSql;

        sqls.add(createViewSqlTmp);
        sqls.add(createViewSql);

        updateSource();
        return sqls;
    }


    public void updateSource() {
        FlinkTableQueueService flinkTableQueueService = SpringUtil.getBean(FlinkTableQueueService.class);
        Map<String, List<FlinkSourceColumn>> tableColumns = new ConcurrentHashMap<>(8);
        tableColumns.put(this.currentTableName, this.currentColumns);
        flinkTableQueueService.put(this.jobId, this.componentId, tableColumns);
    }
}
